//
//  AliMNSTopic.m
//  AlibabaCloudMNSiOS
//
//  Created by David Chen on 28/03/2017.
//  Copyright © 2017 AliCloud. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AliMNSTopic.h"

@implementation AliMNSTopic
@synthesize topicName;
@synthesize mMNSClient;

-(id) initWithTopicName:(NSString*) pTopicName
          withMNSClient:(AliMNSClient*) pMNSClient
{
    topicName = pTopicName;
    mMNSClient = pMNSClient;

    return self;
}

-(NSString*) create:(AliMNSTopicMeta*) topicMeta
{
    AliMNSCreateTopicRequest* ctreq = [[AliMNSCreateTopicRequest alloc] initWithTopicName: topicMeta.topicName];
    AliMNSCreateTopicResponse* ctres = [AliMNSCreateTopicResponse new];

    // build CreateRequest from topicMeta.
    ctreq.maximumMessageSize = topicMeta.maximumMessageSize;

    // client createQueue
    [mMNSClient sendRequest: ctreq withResponse: ctres];

    //parse result from cqres.
    return ctres.topicUrl;
}

-(void) deleteTopic
{
    AliMNSDeleteTopicRequest* dtreq = [[AliMNSDeleteTopicRequest alloc] initWithTopicName:topicName];
    AliMNSDeleteTopicResponse* dtres = [AliMNSDeleteTopicResponse new];

    [mMNSClient sendRequest: dtreq withResponse: dtres];
}

-(void) setTopicAttributes:(AliMNSTopicMeta*) topicMeta
{
    AliMNSSetTopicAttributesRequest* staReq = [[AliMNSSetTopicAttributesRequest alloc] initWithTopicName:topicMeta.topicName withMessageSize:topicMeta.maximumMessageSize];
    staReq.loggingEnable = topicMeta.loggingEnable;

    AliMNSSetTopicAttributesResponse* staRes = [AliMNSSetTopicAttributesResponse new];

    [mMNSClient sendRequest:staReq withResponse:staRes];
}

-(AliMNSTopicMeta*) getTopicAttributes
{
    AliMNSGetTopicAttributesRequest* gtaReq = [[AliMNSGetTopicAttributesRequest alloc] initWithTopicName:self.topicName];
    AliMNSGetTopicAttributesResponse* gtaRes = [AliMNSGetTopicAttributesResponse new];

    [mMNSClient sendRequest:gtaReq withResponse: gtaRes];

    return gtaRes.topicMeta;
}

-(AliMNSTopicMessage*) publishMessage:(AliMNSTopicMessage*) message
{
    if(message == nil || message.messageBody == nil)
    {
        return nil;
    }
    AliMNSPublishMessageRequest* pmReq = [[AliMNSPublishMessageRequest alloc] initWithTopicName:self.topicName withMessageBody:message.messageBody withMessageTag:message.messageTag];
    AliMNSPublishMessageResponse* pmRes = [AliMNSPublishMessageResponse new];

    [mMNSClient sendRequest:pmReq withResponse:pmRes];

    return pmRes.message;
}

-(void) asyncPublishMessage:(AliMNSTopicMessage*) message
         withSuccessHandler:(void (^)(AliMNSTopicMessage* msg)) successHandler
            withFailHandler:(void (^)(AliMNSError* err)) failHandler
{
    if(message == nil || message.messageBody == nil)
    {
        return;
    }
    AliMNSPublishMessageRequest* pmReq = [[AliMNSPublishMessageRequest alloc] initWithTopicName:self.topicName withMessageBody:message.messageBody withMessageTag:message.messageTag];
    AliMNSPublishMessageResponse* pmRes = [AliMNSPublishMessageResponse new];

    [mMNSClient sendAyncRequest:pmReq withResponse: pmRes withResponseHanlder:^(AliMNSResponseBase *asyncRes) {
        AliMNSPublishMessageResponse* inRes = (AliMNSPublishMessageResponse*)asyncRes;
        AliMNSTopicMessage* msg = inRes.message;

        if(msg != nil)
        {
            successHandler(msg);
        }else
        {
            AliMNSError* e = [inRes generateError];
            failHandler(e);
        }
    }];
}

-(NSString*) subscribe:(AliMNSSubscription*) subscription
{
    AliMNSSubscribeRequest* subReq = [[AliMNSSubscribeRequest alloc] initWithTopicName:self.topicName withSubscriptionName:subscription.subscriptionName withEndpoint:subscription.endpoint];

    subReq.filterTag = subscription.filterTag;
    subReq.notifyStrategy = subscription.notifyStrategy;
    subReq.notifyContentFormat = subscription.notifyContentFormat;

    AliMNSSubscribeResponse* subRes = [AliMNSSubscribeResponse new];

    [mMNSClient sendRequest:subReq withResponse:subRes];

    return subRes.subscriptionUrl;
}

-(void) unsubscribe:(AliMNSSubscription*)subscription
{
    AliMNSUnsubcribeRequest* usubReq = [[AliMNSUnsubcribeRequest alloc] initWithTopicName:self.topicName withSubscriptionName:subscription.subscriptionName];

    AliMNSSubscribeResponse* usubRes = [AliMNSSubscribeResponse new];

    [mMNSClient sendRequest:usubReq withResponse:usubRes];
}

-(NSArray*) listSubscription:(NSString*) prefix
            withReturnNumber:(NSInteger)returnNumber
                  withMarker:(NSString**) marker
{
    AliMNSListSubscriptionRequest* lsubReq = [[AliMNSListSubscriptionRequest alloc] initWithTopicName:self.topicName];
    lsubReq.prefix = prefix;
    lsubReq.returnNumber = returnNumber;
    lsubReq.marker = *marker;

    AliMNSListSubscriptionResponse* lsubRes = [AliMNSListSubscriptionResponse new];

    [mMNSClient sendRequest:lsubReq withResponse:lsubRes];

    *marker = lsubRes.nextMarker;

    return lsubRes.subscriptionUrlList;
}

-(void) setSubscriptionAttributes:(AliMNSSubscription*) sub
{
    AliMNSSetSubscriptionAttributesRequest* ssubaReq = [[AliMNSSetSubscriptionAttributesRequest alloc] initWithTopicName:self.topicName withSubscriptionName:sub.subscriptionName];

    // only support to modify the notify strategy.
    ssubaReq.notifyStrategy = sub.notifyStrategy;

    AliMNSSetSubscriptionAttributesResponse* ssubaRes = [AliMNSSetSubscriptionAttributesResponse new];

    [mMNSClient sendRequest:ssubaReq withResponse:ssubaRes];
}

-(AliMNSSubscription*) getSubscriptionAttributes:(NSString*) subName
{
    AliMNSGetSubscriptionAttributesRequest* gsubaReq = [[AliMNSGetSubscriptionAttributesRequest alloc] initWithTopicName:self.topicName withSubscriptionName:subName];

    AliMNSGetSubscriptionAttributesResponse* gsubaRes = [AliMNSGetSubscriptionAttributesResponse new];

    [mMNSClient sendRequest:gsubaReq withResponse:gsubaRes];

    return gsubaRes.subscriptionMeta;
}

@end
